/**
 * Copyright (C) 2010, 2011 Neofonie GmbH
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package eu.dicodeproject.blogimport.filereader;

import java.io.IOException;
import javax.xml.stream.XMLStreamException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import eu.dicodeproject.blogimport.parser.StringUtil;

/**
 * This {@link FileSource} implementation is relatively straight forward. It
 * scans the referenced directory for xml files with the usage of an polling
 * algorithm. This polling needs to be stopped after the FileSource has been
 * used by calling shutdown().
 * 
 * 
 * 
 */
public class XMLFileSource implements FileSource {

  private static final Logger LOG = LoggerFactory.getLogger(XMLFileSource.class);

  private String xmlDir;

  private String automoveDir;

  private PollingFileSystemReader reader;

  /**
   * C'tor that creates a polling source on a referenced directory.
   * 
   * @param dir
   *          the path to the directory as {@link String}
   */
  public XMLFileSource(final String dir) {
    if (dir == null) {
      throw new IllegalArgumentException("No path referenced.");
    }
    this.xmlDir = dir;
    this.reader = new PollingFileSystemReader();
  }

  /**
   * C'tor that creates a polling source on a referenced directory.
   * 
   * @param dir
   *          the path to the directory as {@link String}
   * @param automoveDir
   *          the path to the target directory where the files should be moved
   *          to
   */
  public XMLFileSource(final String dir, final String automoveDir) {
    if (dir == null) {
      throw new IllegalArgumentException("No path referenced.");
    }
    if (automoveDir == null) {
      throw new IllegalArgumentException("No move path referenced.");
    }
    this.xmlDir = dir;
    this.automoveDir = automoveDir;
    this.reader = new PollingFileSystemReader();
  }

  /**
   * @see eu.dicodeproject.blogimport.filereader.FileSource#init()
   */
  @Override
  public boolean init() {
    this.reader.initialize(this.xmlDir, this.automoveDir);
    return true;
  }

  /**
   * @see eu.dicodeproject.blogimport.filereader.FileSource#hasNext()
   */
  @Override
  public boolean hasNext() {
    return reader.hasNext();
  }

  /**
   * @see eu.dicodeproject.blogimport.filereader.FileSource#next()
   */
  @Override
  public String next() {
    String content = null;
    try {
      content = StringUtil.readString(this.reader.getNext());
    } catch (XMLStreamException e) {
      LOG.error("Error parsing file.");
    } catch (IOException e) {
      LOG.error("Can't read file.");
    } catch (InterruptedException e) {
      e.printStackTrace();
      LOG.error("Interruped " + e.getMessage());
    }
    return content;
  }

  /**
   * @see eu.dicodeproject.blogimport.filereader.FileSource#shutdown()
   */
  @Override
  public void shutdown() {
    try {
      this.reader.close();
    } catch (IOException e) {
      LOG.error("Can't shutdown Reader. ", e);
    }
  }

  /**
   * Gets the path to the initially referenced xml dir.
   * 
   * @return the xml dir
   */
  public String getXmlDir() {
    return this.xmlDir;
  }
}
